<?php
class Context extends CI_Model 
{
	function creatContext ($context)
	{
		
		$insert= $this->db->insert('context',$context); //inserting the array in the database
		return $insert;
		
	}
        function getSituation_id($cid)
	{
                //SELECT part is omitted because its SELECT *
                $this->db->select('situation_Id');
                $this->db->from('context');
                $this->db->where('id',$cid);
                //$this->db->order_by("modificationDate", "asc");
                $quere=$this->db->get();

                return $quere;
	}
        function getMyContext($sid)
	{
                //SELECT part is omitted because its SELECT *
                $this->db->select('id,name,html,color,tags,situation_Id,flag');
                $this->db->from('context');
                $this->db->where('situation_Id',$sid);
                $this->db->order_by("modificationDate", "asc");
                $quere=$this->db->get();

                return $quere;
	}
        function get_last_id ()
	{
		$query = $this->db->query("SELECT LAST_INSERT_ID()");
		return $query;
	}
	function deleteContext($cid)
	{
		if ($cid == NULL)
		{
			return false;
		}
		else 
		{
                    $this->db->where('id', $cid);
                    $delete=$this->db->delete('context');
		}
		return $delete ;
	}
	
	function customizeContext($cid,$context)
	{

            $this->db->where('id', $cid);
            $update= $this->db->update('context',$context ); //the third parameter is for WHERE claus
            return $update;
                /*
		$data = array ('height'=> $height,'width' => $width,'xPoint'=> $xpoint,'yPoint'=> $ypoint);
		$update= $this->db->update('context',$data,array('id'=>$cid,'situation_id'=>$sid) ); //the third parameter is for WHERE claus
		return $update;*/
	}
	
	function addComponent($cid,$componentname)
	{
		// i'm assuming that the context id will be given from a field.
		if($componentName==null)
		{
			echo 'the component name is empty...Good Bye';
		}
		else 
		{
			$data= array ('webComponentName' => $componentName);
			$this ->db-> where('Context_id', $cid);
			
			$update = $this->db-> update('mediaobject',$data);
			return $update;
		}
	}
	
	//The following function is done by Basim
	function smartSearch($context, $choice)
	{
		/*Notes:
		- at the client side, there should be a validation that the user have selected at least one context to
		search for.
		- This function will take two parameters. The first parameter is all the selected context names stored in an
		array and the second parameter is the user search choice (1 for  search in puplic & friend profiles, 2 for
		search in the internet and 3 for search in both (for more details, review SRS document, page 57))
		
		- VERY IMPORTANT NOTE: WHY THERE IS NO CONTEXT_NAME ATTRIBUTE IN THE DDL!! THERE IS ONLY CONTEXT ID!!
		WE HAVE TO ADD IT, OTHERWISE HOW SMART SEARCH WILL WORK? IT WILL SEARCH BY WHAT?!
		*/
		
		if($choice == 1)
		{
			for($i=0 ; $i<$context.length() ; $i++)
			{
				//searching in public situations
				$context_name = $context[$i];
				//SELECT part is omitted because it is SELECT *
				$this->db->from('context');
				$this->db->where('contextName',$context_name); // I assumed that there is an attribute called contextName
				$this->db->where('privacyType','public');
				$quere=$this->db->get();
				
				//search in friend's profile
				// I am still thinking on how to access the friend list
			}
		}
		
		else if($choice == 2)
		{
			//I really don't know how te search in the internet
		}
		
		else if($choice == 3)
		{
			//searching in public situations
				$context_name = $context[$i];
				//SELECT part is omitted because it is SELECT *
				$this->db->from('context');
				$this->db->where('contextName',$context_name); // I assumed that there is an attribute called contextName
				$this->db->where('privacyType','public');
				$quere=$this->db->get();
				
				//search in friend's profile
				// I am still thinking on how to access the friend list
				//I really don't know how te search in the internet
		}
	}
        function copyContext($data) // CALL ME ON MY PHONE
        {
                $query= $this->db->insert('context',$data);
                return $query;
        }

        function editContext($cid,$title,$tags,$color,$html)
	{

            $this->db->where('id', $cid);


            $data = array ('name'=> $title,'tags' => $tags,'color'=> $color,'html'=>$html);
            $update= $this->db->update('context',$data ); //the third parameter is for WHERE claus
            return $update;
	}

        function getContext($cid){
            $this->db->where('id', $cid);
            $quere=$this->db->get('context');
            return $quere;
        }
}